//
//  InvoiceInfoViewController.m
//  Hxcsfw
//
//  Created by 企友软mac on 2018/1/15.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "InvoiceInfoViewController.h"
#import "IQKeyboardManager.h"

@interface InvoiceInfoViewController ()<UITextFieldDelegate> 

@property (weak, nonatomic) IBOutlet UITextField *txtName;
@property (weak, nonatomic) IBOutlet UITextField *txtPhone;
@property (weak, nonatomic) IBOutlet UITextField *txtTicketName;
@property (weak, nonatomic) IBOutlet UITextField *txtNum;
@property (weak, nonatomic) IBOutlet UITextField *txtMailbox;
@property (weak, nonatomic) IBOutlet UITextField *txtExplain;
@property (weak, nonatomic) IBOutlet UIView *infoView;

@property (weak, nonatomic) IBOutlet UIButton *btnKai;
@property (weak, nonatomic) IBOutlet UIButton *btnNoKai;

@end

@implementation InvoiceInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"back_icon.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 48);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];

    if ([WShareUtil sharedData].isHaveInvoice) {
        [self.btnNoKai setBackgroundImage:[UIImage imageNamed:@"fapiao_wxz.png"] forState:UIControlStateNormal];
        [self.btnKai setBackgroundImage:[UIImage imageNamed:@"fapiao_xz.png"] forState:UIControlStateNormal];
        [self.btnKai setTitleColor:WRGBColor(234, 52, 6) forState:UIControlStateNormal];
        [self.btnNoKai setTitleColor:WRGBColor(0, 0, 0) forState:UIControlStateNormal];
        self.infoView.hidden = NO;

    } else {
        [self.btnKai setBackgroundImage:[UIImage imageNamed:@"fapiao_wxz.png"] forState:UIControlStateNormal];
        [self.btnNoKai setBackgroundImage:[UIImage imageNamed:@"fapiao_xz.png"] forState:UIControlStateNormal];
        [self.btnNoKai setTitleColor:WRGBColor(234, 52, 6) forState:UIControlStateNormal];
        [self.btnKai setTitleColor:WRGBColor(0, 0, 0) forState:UIControlStateNormal];
        self.infoView.hidden = YES;
    }

    self.txtName.delegate = self;
    self.txtPhone.delegate = self;
    self.txtTicketName.delegate = self;
    self.txtNum.delegate = self;
    self.txtMailbox.delegate = self;
    self.txtExplain.delegate = self;

}

- (void)back {
    [WShareUtil sharedData].isHaveInvoice = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnKaiClick:(UIButton *)sender {
    if (sender.tag == 1000) {

        [WShareUtil sharedData].isHaveInvoice = YES;
        [WShareUtil sharedData].dicMinvoice = [NSMutableDictionary dictionary];
        self.infoView.hidden = NO;
        [self.btnKai setBackgroundImage:[UIImage imageNamed:@"fapiao_xz.png"] forState:UIControlStateNormal];
        [self.btnNoKai setBackgroundImage:[UIImage imageNamed:@"fapiao_wxz.png"] forState:UIControlStateNormal];
        [self.btnKai setTitleColor:WRGBColor(234, 52, 6) forState:UIControlStateNormal];
        [self.btnNoKai setTitleColor:WRGBColor(0, 0, 0) forState:UIControlStateNormal];
    } else if (sender.tag == 1001) {

        [WShareUtil sharedData].isHaveInvoice = NO;
        [WShareUtil sharedData].dicMinvoice = nil;
        self.infoView.hidden = YES;
        [self.btnKai setBackgroundImage:[UIImage imageNamed:@"fapiao_wxz.png"] forState:UIControlStateNormal];
        [self.btnNoKai setBackgroundImage:[UIImage imageNamed:@"fapiao_xz.png"] forState:UIControlStateNormal];
        [self.btnNoKai setTitleColor:WRGBColor(234, 52, 6) forState:UIControlStateNormal];
        [self.btnKai setTitleColor:WRGBColor(0, 0, 0) forState:UIControlStateNormal];
    } else if (sender.tag == 2000) {

        if (![WShareUtil sharedData].isHaveInvoice) {
            [self.navigationController popViewControllerAnimated:YES];
            return; 
        }

        if ([self checkInput]) {
            [WShareUtil sharedData].dicMinvoice[@"KPLX"] = @"3";
            [WShareUtil sharedData].dicMinvoice[@"productNum"] = @"1";
            [WShareUtil sharedData].dicMinvoice[@"LXR"] = self.txtName.text;
            [WShareUtil sharedData].dicMinvoice[@"LXDH"] = self.txtPhone.text;
            [WShareUtil sharedData].dicMinvoice[@"KPMC"] = self.txtTicketName.text;
            [WShareUtil sharedData].dicMinvoice[@"KPSH"] = self.txtNum.text;
            [WShareUtil sharedData].dicMinvoice[@"EMAIL"] = self.txtMailbox.text;
            [WShareUtil sharedData].dicMinvoice[@"BZ"] = self.txtExplain.text;
            [WShareUtil sharedData].dicMinvoice[@"productId"] = _productId;

            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:NO];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)checkInput{

    BOOL resultBool = NO;

    if ([WShareUtil strNilOrEmpty:self.txtName.text]) {
        self.txtName.text = @"";
        [WShareUtil showAlert:@"温馨提示" message:@"请您输入用户名"];
        return resultBool;
    }
    if ([WShareUtil strNilOrEmpty:self.txtPhone.text]) {
        self.txtPhone.text = @"";
        [WShareUtil showAlert:@"温馨提示" message:@"请您输入手机号"];
        return resultBool;
    }else {
        if (![self.txtPhone.text isUserPhone]) {
            [WShareUtil showAlert:@"温馨提示" message:@"请您正确的手机号"];
            return resultBool;
        }
    }
    if ([WShareUtil strNilOrEmpty:self.txtTicketName.text]) {
        self.txtTicketName.text = @"";
        [WShareUtil showAlert:@"温馨提示" message:@"请您输入开票名称"];
        return resultBool;
    }
    if ([WShareUtil strNilOrEmpty:self.txtNum.text]) {
        self.txtNum.text = @"";
        [WShareUtil showAlert:@"温馨提示" message:@"请您输入开票税号"];
        return resultBool;
    }
    if ([WShareUtil strNilOrEmpty:self.txtMailbox.text]) {
        self.txtMailbox.text = @"";
    }
    if ([WShareUtil strNilOrEmpty:self.txtExplain.text]) {
        self.txtExplain.text = @"";
    }
    if ([WShareUtil strNilOrEmpty:self.productId]) {
        self.productId = @"";
    }
    return YES;

}

@end
